package com.imu.wys.controller;


import com.alibaba.fastjson.JSONObject;
import com.imu.wys.model.ZhSen;
import com.imu.wys.service.ZhSenService;
import com.imu.wys.util.SentenceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ZhSen")
public class ZhSenController {
    @Autowired
    ZhSenService zhSenService;



    @RequestMapping(value = {"getSen"})
    public void getSen(HttpServletRequest request, HttpServletResponse response, String senindex, int type) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setCharacterEncoding("UTF-8");

        Map<String, Object> map = new HashMap<>();
        String[] par = senindex.split(";");
        int parentID = Integer.parseInt(par[1]);
        int senIndex = Integer.parseInt(par[0]);
        if(type==1){  //下一题则查找下一个
            senIndex++;
        }else {
            senIndex--;  //否则查找上一个
        }

        ZhSen zhSen = zhSenService.findSen(parentID,senIndex);

        if(zhSen==null){
            map.put("code",201);
        }else {
            map.put("code",200);
        }
        map.put("zhSen",zhSen);
        map.put("index",senIndex+";"+parentID);
        JSONObject json = new JSONObject(map);
        response.getWriter().print(json.toString());
    }


    @RequestMapping(value = {"getArticles"})
    public void getArticles(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setCharacterEncoding("UTF-8");
        Map<String, Object> map = new HashMap<>();
        List<ZhSen> zhSens = zhSenService.findByParentID(-1);
        if(zhSens.size()<=0){
            map.put("code",201);
        }else {
            map.put("code",200);
        }
        map.put("zhSen",zhSens);

        JSONObject json = new JSONObject(map);
        response.getWriter().print(json.toString());
    }


    @RequestMapping(value = {"getArticleMsg"})
    public void getByid(HttpServletRequest request, HttpServletResponse response, int parentID) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setCharacterEncoding("UTF-8");
        Map<String, Object> map = new HashMap<>();
        List<ZhSen> zhSens = zhSenService.findByParentID(parentID);
        map.put("code",200);
        map.put("size",zhSens.size()-1);
        JSONObject json = new JSONObject(map);
        response.getWriter().print(json.toString());
    }


    @RequestMapping(value = {"insert"})
    public void insert(HttpServletRequest request, HttpServletResponse response,String title,String content) throws IOException { //
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setCharacterEncoding("UTF-8");
//        String title = "森林2";
//        String content = "说起森林的功劳，那还多得很。它除了为人类提供木材及许多种生产、生活的原料之外，在维护生态环境方面也是功劳卓著，它用另一种“能吞能吐”的特殊功能孕育了人类。因为地球在形成之初，大气中的二氧化碳含量很高，氧气很少，气温也高，生物是难以生存的。大约在四亿年之前，陆地才产生了森林。森林慢慢将大气中的二氧化碳吸收，同时吐出新鲜氧气，调节气温：这才具备了人类生存的条件，地球上才最终有了人类。\n" +
//                "森林，是地球生态系统的主体，是大自然的总调度室，是地球的绿色之肺。森林维护地球生态环境的这种“能吞能吐”的特殊功能是其他任何物体都不能取代的。然而，由于地球上的燃烧物增多，二氧化碳的排放量急剧增加，使得地球生态环境急剧恶化,主要表现为全球气候变暖，水分蒸发加快，改变了气流的循环，使气候变化加剧，从而引发热浪、飓风、暴雨、洪涝及干旱。";
        //插入标题
        String[] sens = SentenceUtil.splitSentence(content);
        ZhSen zhSen = new ZhSen();
        zhSen.setContent(title);
        zhSen.setParentID(-1);
        zhSen.setSenIndex(0);
        zhSenService.insert(zhSen);
        int id = zhSen.getId();
        //插入标题
        ZhSen zhSenT = new ZhSen();
        zhSenT.setSenIndex(0);
        zhSenT.setContent(title);
        zhSenT.setParentID(id);
        zhSenService.insert(zhSenT);
        //c插入每个句子
        int senIndex=1;
        for(String s :sens){
            ZhSen zhSenS = new ZhSen();
            zhSenS.setSenIndex(senIndex);
            zhSenS.setContent(s.substring(0,s.length()-1));
            zhSenS.setParentID(id);
            zhSenService.insert(zhSenS);
            senIndex++;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("numSen",senIndex);
        JSONObject json = new JSONObject(map);
        response.getWriter().print(json.toString());



    }

}
